package com.ydzh.kfqgwvisitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydzh.kfqgwvisitor.base.Result;
import com.ydzh.kfqgwvisitor.dto.BeVisitorDTO;
import com.ydzh.kfqgwvisitor.entity.BeDepartment;
import com.ydzh.kfqgwvisitor.entity.BeVisitor;
import com.ydzh.kfqgwvisitor.mapper.BeVisitorMapper;
import com.ydzh.kfqgwvisitor.service.BeDepartmentService;
import com.ydzh.kfqgwvisitor.service.BeVisitorService;
import com.ydzh.kfqgwvisitor.vo.BeVisitorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 被访人信息表  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2021-09-01
 */
@Service
public class BeVisitorServiceImpl extends ServiceImpl<BeVisitorMapper, BeVisitor> implements BeVisitorService {

    @Autowired
    private BeVisitorMapper mapper;
    @Autowired
    private BeDepartmentService beDepartmentService;


    @Override
    public List<BeVisitorVO> listToAll(Long unitId, Long departmentId) {
        if (Objects.isNull(unitId)) {
            // 不管有没有传部门都可以使用此sql
            return mapper.listByDeptId(departmentId);
        }
        // 若参数有单位的ID那就使用此sql
        return mapper.findByUnitId(unitId, departmentId);
    }

    @Override
    public Result setTem(Integer turnOnOrOff, Long deptId) {
        if (turnOnOrOff == 1) {
            BeVisitor beVisitor = new BeVisitor();
            beVisitor.setBevisitorId(1L);
            beVisitor.setBevisitorName("lihaoyuan");
            beVisitor.setDeptId(deptId);
            beVisitor.setPhoneNo("18765065318");
            saveOrUpdate(beVisitor);

        } else {
            removeById(1L);
        }
        return Result.buildSuccess();
    }

    @Override
    public Result deleteAll() {
        mapper.deleteAll();
        return Result.buildSuccess();
    }

    @Override
    public Result updateVisitor(Long id, BeVisitorDTO beVisitorDTO) {
        BeVisitor beVisitor = getById(id);
        if (Objects.isNull(beVisitor)) {
            return Result.buildError("id不存在");
        }
        if (StrUtil.isNotBlank(beVisitorDTO.getPhoneNo())) {
            beVisitor.setPhoneNo(beVisitorDTO.getPhoneNo());
        }
        if (StrUtil.isNotBlank(beVisitorDTO.getDeptName())) {
            // 查询部门的ID
            BeDepartment department = beDepartmentService.query().eq("DEPARTMENT_NAME", beVisitorDTO.getDeptName()).one();
            if (Objects.isNull(department)) {
                return Result.buildError(beVisitorDTO.getDeptName() + "--部门不存在");
            }
            beVisitor.setDeptId(department.getBedepartmentId());
        }
        if (StrUtil.isNotBlank(beVisitorDTO.getBevisitorName())) {
            beVisitor.setBevisitorName(beVisitorDTO.getBevisitorName());
        }
        updateById(beVisitor);
        return Result.buildSuccess();
    }

    @Override
    public Result addVisitor(BeVisitorDTO beVisitorDTO) {
        // 先查询手机号是否存在
        Integer num = query().eq("PHONE_NO", beVisitorDTO.getPhoneNo()).count();
        if (num > 0) {
            return Result.buildError("此手机号已存在");
        }
        // 查询部门的ID
        BeDepartment department = beDepartmentService.query().eq("DEPARTMENT_NAME", beVisitorDTO.getDeptName()).one();
        if (Objects.isNull(department)) {
            return Result.buildError(beVisitorDTO.getDeptName() + "--部门不存在");
        }
        BeVisitor beVisitor = BeanUtil.copyProperties(beVisitorDTO, BeVisitor.class);
        beVisitor.setDeptId(department.getBedepartmentId());
        save(beVisitor);
        return Result.buildSuccess();
    }

    @Override
    public Result deleteVisitor(Long visitorId) {
        removeById(visitorId);
        return Result.buildSuccess();
    }

    @Override
    public Result getVisitorInfo(String beVisitorName) {
        List<BeVisitor> list = query().eq("BEVISITOR_NAME", beVisitorName).list();
        return Result.buildSuccess(list);

    }
}
